package com.silences.servlet;

import com.alibaba.fastjson.JSON;
import com.silences.pojo.Buyer;
import com.silences.service.BuyerService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;


@WebServlet("/buyer")
public class BuyerServlet extends HttpServlet {
    //创建对象
    BuyerService buyerService = new BuyerService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        //乱码问题统一解决
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //通过method字段识别方法
        String method =req.getParameter("method");
        if(method.equals("getBuyers")){
            getBuyers(req,resp); //获取商品
        }else if(method.equals("getBuyer")){
            getBuyer(req,resp);//获取单个商品裂帛啊
        }else if(method.equals("addBuyer")){
            addBuyer(req,resp);
        }else if(method.equals("updateBuyer")){
            updateBuyer(req,resp);
        }else if(method.equals("deleteBuyer")){
            deleteBuyer(req,resp);
        }else if(method.equals("getBuyerByName")){
            getBuyerByName(req,resp);
        }
    }



    private void addBuyer(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String buyername =req.getParameter("buyername");
        String password =req.getParameter("password");
        String buyernum =req.getParameter("buyernum");
        String buyerplace =req.getParameter("buyerplace");

        try {
            Buyer buyer = new Buyer(0, buyername, password, buyernum, buyerplace);
            Map<String,Object> map= buyerService.addBuyers(buyer);
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }




    private void getBuyers(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String searchname = req.getParameter("searchname");
        String pageIndex= req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        try {
            Map<String,Object> map = buyerService.getBuyers(searchname, pageIndex, pageSize);
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void getBuyer(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        try {

            Map<String,Object> map= buyerService.getBuyer(Integer.parseInt(id));
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void getBuyerByName(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String buyername = req.getParameter("buyername");
        try {

            Map<String,Object> map= buyerService.getBuyerName(buyername);
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void updateBuyer(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id =req.getParameter("id");
        String buyername =req.getParameter("buyername");
        String password =req.getParameter("password");
        String buyernum =req.getParameter("buyernum");
        String buyerplace =req.getParameter("buyerplace");


        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            Buyer buyer = new Buyer(Integer.parseInt(id) , buyername , password , buyernum , buyerplace);
            Map<String,Object> map= buyerService.updateBuyers(buyer);
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void deleteBuyer(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        try {
            Map<String,Object> map= buyerService.deleteBuyer(Integer.parseInt(id));
            resp.getWriter().write(JSON.toJSON(map).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
